#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ClassInfoProvider : CY.GFive.Core.Providers.DALProvider.IClassInfoProvider
	{
		#region Sql
		private static readonly string SqlSelectClassInfo = "SELECT [Id], [ClassCode],[GradeCode],[ProCode],[OrStdNum] FROM [ClassInfo] WHERE  [Id]=@Id";

		private static readonly string SqlInsertClassInfo = "INSERT INTO [ClassInfo]([ClassCode],[GradeCode],[ProCode],[OrStdNum]) values(@ClassCode,@GradeCode,@ProCode,@OrStdNum);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateClassInfo = "UPDATE [ClassInfo] SET [ClassCode] = @ClassCode,[GradeCode] = @GradeCode,[ProCode] = @ProCode,[OrStdNum] = @OrStdNum WHERE  [Id]=@Id";

		private static readonly string SqlDeleteClassInfo = "DELETE [ClassInfo] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllClassInfo = "SELECT [Id], [ClassCode],[GradeCode],[ProCode],[OrStdNum] FROM [ClassInfo]";
		#endregion

		#region IClassInfoProvider Members
		public Core.Business.ClassInfo Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectClassInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ClassInfo classInfo = new Core.Business.ClassInfo();

				if (!reader.IsDBNull(0)) classInfo.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) classInfo.ClassCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) classInfo.GradeCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) classInfo.ProCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) classInfo.OrStdNum = reader.GetInt32(4);

				return classInfo;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ClassInfo classInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, classInfo.ClassCode);
			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, classInfo.GradeCode);
			sql.AddParameter("@ProCode", SqlDbType.NVarChar, classInfo.ProCode);
			sql.AddParameter("@OrStdNum", SqlDbType.Int, classInfo.OrStdNum);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertClassInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) classInfo.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ClassInfo classInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@ClassCode", SqlDbType.NVarChar, classInfo.ClassCode);
			sql.AddParameter("@GradeCode", SqlDbType.NVarChar, classInfo.GradeCode);
			sql.AddParameter("@ProCode", SqlDbType.NVarChar, classInfo.ProCode);
			sql.AddParameter("@OrStdNum", SqlDbType.Int, classInfo.OrStdNum);
			sql.AddParameter("@Id", SqlDbType.Int, classInfo.Id);
			sql.ExecuteSql(SqlUpdateClassInfo);
		}

		public void Delete(Core.Business.ClassInfo classInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, classInfo.Id);
			sql.ExecuteSql(SqlDeleteClassInfo);
		}

		public IList<Core.Business.ClassInfo> GetAllClassInfo()
		{
			IList<Core.Business.ClassInfo> classInfolist = new List<Core.Business.ClassInfo>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllClassInfo);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ClassInfo classInfo = new Core.Business.ClassInfo();

					if (!reader.IsDBNull(0)) classInfo.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) classInfo.ClassCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) classInfo.GradeCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) classInfo.ProCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) classInfo.OrStdNum = reader.GetInt32(4);

					classInfo.MarkOld();
					classInfolist.Add(classInfo);
				}
				reader.Close();
			}
			return classInfolist;
		}
		#endregion
	}
}
